 package tools;

 import java.util.Vector;










 public class CaltechEval
 {
   final String expression;
   String[] Operators;
   int[] OperatorsArgs;
   boolean[] OperatorsLeftRight;
   int[] OperatorsPrecedence;
   int NOPERATORS;

   public CaltechEval(String s) {
/*  24 */     init();
/*  25 */     this.expression = s.replace(" ", "");
   }

   public static void main(String[] args) {
/*  29 */     if (args == null || args.length != 1) {
/*  30 */       System.exit(0);
     }

/*  33 */     CaltechEval m = new CaltechEval(args[0]);
   }


   private void init() {
/*  38 */     this.NOPERATORS = 20;
/*  39 */     this.Operators = new String[this.NOPERATORS];
/*  40 */     this.OperatorsArgs = new int[this.NOPERATORS];
/*  41 */     this.OperatorsLeftRight = new boolean[this.NOPERATORS];
/*  42 */     this.OperatorsPrecedence = new int[this.NOPERATORS];

/*  44 */     this.Operators[0] = "+";
/*  45 */     this.OperatorsArgs[0] = 2;
/*  46 */     this.OperatorsLeftRight[0] = true;
/*  47 */     this.OperatorsPrecedence[0] = 2;

/*  49 */     this.Operators[1] = "-";
/*  50 */     this.OperatorsArgs[1] = 2;
/*  51 */     this.OperatorsLeftRight[1] = true;
/*  52 */     this.OperatorsPrecedence[1] = 2;

/*  54 */     this.Operators[2] = "*";
/*  55 */     this.OperatorsArgs[2] = 2;
/*  56 */     this.OperatorsLeftRight[2] = true;
/*  57 */     this.OperatorsPrecedence[2] = 3;

/*  59 */     this.Operators[3] = "/";
/*  60 */     this.OperatorsArgs[3] = 2;
/*  61 */     this.OperatorsLeftRight[3] = true;
/*  62 */     this.OperatorsPrecedence[3] = 3;

/*  64 */     this.Operators[4] = "sin";
/*  65 */     this.OperatorsArgs[4] = 1;
/*  66 */     this.OperatorsLeftRight[4] = false;
/*  67 */     this.OperatorsPrecedence[4] = 10;

/*  69 */     this.Operators[5] = "cos";
/*  70 */     this.OperatorsArgs[5] = 1;
/*  71 */     this.OperatorsLeftRight[5] = false;
/*  72 */     this.OperatorsPrecedence[5] = 10;

/*  74 */     this.Operators[6] = "tan";
/*  75 */     this.OperatorsArgs[6] = 1;
/*  76 */     this.OperatorsLeftRight[6] = false;
/*  77 */     this.OperatorsPrecedence[6] = 10;

/*  79 */     this.Operators[7] = "exp";
/*  80 */     this.OperatorsArgs[7] = 1;
/*  81 */     this.OperatorsLeftRight[7] = false;
/*  82 */     this.OperatorsPrecedence[7] = 10;

/*  84 */     this.Operators[8] = "sqrt";
/*  85 */     this.OperatorsArgs[8] = 1;
/*  86 */     this.OperatorsLeftRight[8] = false;
/*  87 */     this.OperatorsPrecedence[8] = 10;

/*  89 */     this.Operators[9] = "asin";
/*  90 */     this.OperatorsArgs[9] = 1;
/*  91 */     this.OperatorsLeftRight[9] = false;
/*  92 */     this.OperatorsPrecedence[9] = 10;

/*  94 */     this.Operators[10] = "acos";
/*  95 */     this.OperatorsArgs[10] = 1;
/*  96 */     this.OperatorsLeftRight[10] = false;
/*  97 */     this.OperatorsPrecedence[10] = 10;

/*  99 */     this.Operators[11] = "atan";
/* 100 */     this.OperatorsArgs[11] = 1;
/* 101 */     this.OperatorsLeftRight[11] = false;
/* 102 */     this.OperatorsPrecedence[11] = 10;

/* 104 */     this.Operators[12] = "atan2";
/* 105 */     this.OperatorsArgs[12] = 2;
/* 106 */     this.OperatorsLeftRight[12] = false;
/* 107 */     this.OperatorsPrecedence[12] = 10;

/* 109 */     this.Operators[13] = "max";
/* 110 */     this.OperatorsArgs[13] = 2;
/* 111 */     this.OperatorsLeftRight[13] = false;
/* 112 */     this.OperatorsPrecedence[13] = 10;

/* 114 */     this.Operators[14] = "min";
/* 115 */     this.OperatorsArgs[14] = 2;
/* 116 */     this.OperatorsLeftRight[14] = false;
/* 117 */     this.OperatorsPrecedence[14] = 10;

/* 119 */     this.Operators[15] = ",";
/* 120 */     this.OperatorsArgs[15] = 2;
/* 121 */     this.OperatorsLeftRight[15] = true;
/* 122 */     this.OperatorsPrecedence[15] = 1;

/* 124 */     this.Operators[16] = "u";
/* 125 */     this.OperatorsArgs[16] = 1;
/* 126 */     this.OperatorsLeftRight[16] = false;
/* 127 */     this.OperatorsPrecedence[16] = 4;

/* 129 */     this.Operators[17] = "d";
/* 130 */     this.OperatorsArgs[17] = 1;
/* 131 */     this.OperatorsLeftRight[17] = false;
/* 132 */     this.OperatorsPrecedence[17] = 4;

/* 134 */     this.Operators[18] = "n";
/* 135 */     this.OperatorsArgs[18] = 1;
/* 136 */     this.OperatorsLeftRight[18] = false;
/* 137 */     this.OperatorsPrecedence[18] = 11;

/* 139 */     this.Operators[19] = "log";
/* 140 */     this.OperatorsArgs[19] = 2;
/* 141 */     this.OperatorsLeftRight[19] = false;
/* 142 */     this.OperatorsPrecedence[19] = 11;
   }

   public double evaluate() {
/* 146 */     return eval(this.expression);
   }

   private int getOperator(String op) {
/* 150 */     for (int i = 0; i < this.NOPERATORS; i++) {
/* 151 */       if (op.equals(this.Operators[i]) && op.length() == this.Operators[i].length()) {
/* 152 */         return i;
       }
     }
/* 155 */     return -1;
   }

   private double doOp(String op, double arg1, double arg2) {
/* 159 */     if (op.equals("+")) {
/* 160 */       return arg1 + arg2;
     }
/* 162 */     if (op.equals("-")) {
/* 163 */       return arg1 - arg2;
     }
/* 165 */     if (op.equals("*")) {
/* 166 */       return arg1 * arg2;
     }
/* 168 */     if (op.equals("/")) {
/* 169 */       return arg1 / arg2;
     }
/* 171 */     if (op.equals("sin")) {
/* 172 */       return Math.sin(arg1);
     }
/* 174 */     if (op.equals("cos")) {
/* 175 */       return Math.cos(arg1);
     }
/* 177 */     if (op.equals("tan")) {
/* 178 */       return Math.tan(arg1);
     }
/* 180 */     if (op.equals("exp")) {
/* 181 */       return Math.exp(arg1);
     }
/* 183 */     if (op.equals("sqrt")) {
/* 184 */       return Math.sqrt(arg1);
     }
/* 186 */     if (op.equals("asin")) {
/* 187 */       return Math.asin(arg1);
     }
/* 189 */     if (op.equals("acos")) {
/* 190 */       return Math.acos(arg1);
     }
/* 192 */     if (op.equals("atan")) {
/* 193 */       return Math.atan(arg1);
     }
/* 195 */     if (op.equals("atan2")) {
/* 196 */       return Math.atan2(arg1, arg2);
     }
/* 198 */     if (op.equals("max")) {
/* 199 */       return Math.max(arg1, arg2);
     }
/* 201 */     if (op.equals("min")) {
/* 202 */       return Math.min(arg1, arg2);
     }
/* 204 */     if (op.equals("u")) {
/* 205 */       return Math.ceil(arg1);
     }
/* 207 */     if (op.equals("d")) {
/* 208 */       return Math.floor(arg1);
     }
/* 210 */     if (op.equals("n")) {
/* 211 */       return -arg1;
     }
/* 213 */     if (op.equals("log")) {
/* 214 */       return Math.log(arg2) / Math.log(arg1);
     }
/* 216 */     throw new RuntimeException("Invalid operator: " + op);
   }

   private boolean inOperand(char c) {
/* 220 */     return ((c >= '0' && c <= '9') || c == '.');
   }

   private Vector<String> toTokens(String s) {
/* 224 */     Vector<String> tokenStrings = new Vector<>(1);
/* 225 */     String operand = "";
/* 226 */     String operator = "";
/* 227 */     int nd = 0;
/* 228 */     int nt = 0;
/* 229 */     int i = 0;
/* 230 */     while (i < s.length()) {
/* 231 */       char c = s.charAt(i);
/* 232 */       char cnext = ' ';

/* 234 */       if (i < s.length() - 1) {
/* 235 */         cnext = s.charAt(i + 1);
       }

/* 238 */       if (nt > 0 && operator.equals("atan") && c == '2') {

/* 240 */         tokenStrings.addElement("atan2");
/* 241 */         operator = "";
/* 242 */         nt = 0;
/* 243 */         i++; continue;
/* 244 */       }  if (inOperand(c)) {

/* 246 */         operand = operand + s.charAt(i);
/* 247 */         nd++;
/* 248 */         if (nt > 0) {
/* 249 */           tokenStrings.addElement(operator);
         }
/* 251 */         nt = 0;
/* 252 */         operator = "";
/* 253 */         i++; continue;
/* 254 */       }  if (c == 'e' && nd > 0 && i < s.length() - 2 && (cnext == '+' || cnext == '-' || 
/* 255 */         inOperand(cnext))) {

/* 257 */         operand = operand + c;
/* 258 */         operand = operand + cnext;
/* 259 */         nd += 2;
/* 260 */         nt = 0;
/* 261 */         i += 2; continue;
/* 262 */       }  if (c == '(' || c == ')') {

/* 264 */         if (nd > 0) {
/* 265 */           tokenStrings.addElement(operand);
         }
/* 267 */         nd = 0;
/* 268 */         operand = "";
/* 269 */         if (nt > 0) {
/* 270 */           tokenStrings.addElement(operator);
         }
/* 272 */         operator = "" + c;
/* 273 */         tokenStrings.addElement(operator);
/* 274 */         nt = 0;
/* 275 */         operator = "";
/* 276 */         i++;
         continue;
       }
/* 279 */       if (nd > 0) {
/* 280 */         tokenStrings.addElement(operand);
       }
/* 282 */       nd = 0;
/* 283 */       operand = "";
/* 284 */       operator = operator + c;
/* 285 */       if (operator.equals("+") || operator.equals("-") || operator
/* 286 */         .equals("*") || operator.equals("/") || operator
/* 287 */         .equals(",")) {
/* 288 */         tokenStrings.addElement(operator);
/* 289 */         nt = 0;
/* 290 */         operator = "";
       } else {
/* 292 */         nt++;
       }
/* 294 */       i++;
     }

/* 297 */     if (nd > 0) {
/* 298 */       tokenStrings.addElement(operand);
/* 299 */       nt = 0;
     }
/* 301 */     if (nt > 0) {
/* 302 */       tokenStrings.addElement(operator);
     }
/* 304 */     return tokenStrings;
   }




   private double eval(String s) {
/* 311 */     Vector<String> tokens = toTokens(s);









/* 321 */     while (tokens.size() > 1) {
/* 322 */       tokens = reduceTokens(tokens);
     }

/* 325 */     return Double.parseDouble(tokens.elementAt(0));
   }



   public Vector<String> reduceTokens(Vector<String> tokens) {
/* 331 */     double leftValue = 0.0D;




/* 336 */     while (tokens.indexOf("(") != -1) {
/* 337 */       int ib = tokens.indexOf("(");
/* 338 */       Vector<String> bracketTokens = new Vector<>();
/* 339 */       int brackets = 1;
/* 340 */       for (int it = ib + 1; it < tokens.size(); it++) {
/* 341 */         String st = tokens.elementAt(it);
/* 342 */         if (st.equals(")")) {
/* 343 */           brackets--;
/* 344 */         } else if (st.equals("(")) {
/* 345 */           brackets++;
         }
/* 347 */         if (brackets == 0) {


/* 350 */           for (int i3 = ib + 1; i3 < it; i3++) {
/* 351 */             bracketTokens.addElement(tokens.elementAt(i3));
           }
/* 353 */           int startsize = bracketTokens.size();




/* 358 */           bracketTokens = reduceTokens(bracketTokens);
/* 359 */           int endsize = bracketTokens.size();





/* 365 */           int ip = ib;
/* 366 */           for (int j = 0; j < bracketTokens.size(); j++) {
/* 367 */             tokens.setElementAt(bracketTokens.elementAt(j), ip++);
           }
/* 369 */           for (int i = 0; i < startsize - endsize + 2; i++) {
/* 370 */             tokens.removeElementAt(ip);
           }





           break;
         }
       }
     }





/* 386 */     while (tokens.size() > 1) {






/* 393 */       int maxprec = 0;
/* 394 */       int ipos = -1; int it;
/* 395 */       for (it = 0; it < tokens.size(); it++) {
/* 396 */         String str = tokens.elementAt(it);
/* 397 */         int i = getOperator(str);
/* 398 */         if (i != -1)
         {

/* 401 */           if (this.OperatorsPrecedence[i] >= maxprec) {
/* 402 */             maxprec = this.OperatorsPrecedence[i];
/* 403 */             ipos = it;
           }
         }
       }
/* 407 */       if (ipos == -1) {
/* 408 */         return tokens;
       }
/* 410 */       it = ipos;

/* 412 */       String st = tokens.elementAt(it);
/* 413 */       int iop = getOperator(st);


/* 416 */       if (this.OperatorsLeftRight[iop]) {
/* 417 */         if (!st.equals(",")) {
/* 418 */           int ipt = it - 1;
/* 419 */           if (it > 0) {
/* 420 */             String stleft = tokens.elementAt(it - 1);
/* 421 */             if (getOperator(stleft) != -1) {
/* 422 */               ipt = it;
/* 423 */               leftValue = 0.0D;
             } else {
/* 425 */               leftValue = Double.parseDouble(stleft);
             }
           } else {
/* 428 */             ipt = 0;
/* 429 */             leftValue = 0.0D;
           }
/* 431 */           String str = tokens.elementAt(it + 1);
/* 432 */           double d1 = Double.parseDouble(str);

/* 434 */           double d2 = doOp(st, leftValue, d1);
/* 435 */           str = "" + d2;

/* 437 */           tokens.setElementAt(str, ipt);

/* 439 */           tokens.removeElementAt(ipt + 1);
/* 440 */           if (it > 0 && ipt != it) {
/* 441 */             tokens.removeElementAt(ipt + 1);
           }

           continue;
         }
/* 446 */         tokens.removeElementAt(it);
         continue;
       }
/* 449 */       int nargs = this.OperatorsArgs[iop];
/* 450 */       String stright = tokens.elementAt(it + 1);
/* 451 */       double rightValue1 = Double.parseDouble(stright);
/* 452 */       double rightValue2 = 0.0D;
/* 453 */       if (nargs > 1) {
/* 454 */         stright = tokens.elementAt(it + 2);
/* 455 */         rightValue2 = Double.parseDouble(stright);
       }


/* 459 */       double value = doOp(st, rightValue1, rightValue2);
/* 460 */       stright = "" + value;

/* 462 */       tokens.setElementAt(stright, it);


/* 465 */       tokens.removeElementAt(it + 1);
/* 466 */       if (nargs > 1) {
/* 467 */         tokens.removeElementAt(it + 1);
       }
     }









/* 479 */     return tokens;
   }
 }


/* Location:              C:\PGX\Server.jar!\tools\CaltechEval.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */